package com.jlastu.dlxy_androidapp.presenter;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.jlastu.dlxy_androidapp.contract.AddSchoolContract;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;
import com.jlastu.dlxy_androidapp.view.main.AddSchoolActivity;

import java.util.List;

public class AddSchoolPresenter implements AddSchoolContract.IAddSchoolPresenter {
    private AddSchoolContract.IAddSchoolView mAddSchoolView;

    private Context mContext;

    public AddSchoolPresenter(AddSchoolContract.IAddSchoolView addSchoolView, Context context) {
        mAddSchoolView = addSchoolView;
        mContext = context;

    }

    @Override
    public void saveSchool(School school) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                //商品插入
                ContentValues cv = new ContentValues();
                cv.put("school_name", school.getName());
                cv.put("school_know", school.getKnow());
                cv.put("school_classify", school.getClassify());
                Uri schoolUrl = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/school");
                Uri insertUri = mContext.getContentResolver().insert(schoolUrl, cv);
                long newRowId = ContentUris.parseId(insertUri);
                Log.d("", "新插入的文章id ===== " + newRowId);
                List<SchoolPhoto> photoList = school.getSchoolphotoList();
                Log.d("", "photoList ===== " + photoList.size());
                ContentValues[] arrayValues = new ContentValues[photoList.size()];
                for (int i = 0; i < photoList.size() -1; i++) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("school_id", newRowId);
                    contentValues.put("image", photoList.get(i).getData());
                    arrayValues[i] = contentValues;
                    Log.d("", "arrayValues ===== " + i);
                }
                Uri schoolphotoUrl = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/schoolphoto");
                //批量插入图片
                int count = mContext.getContentResolver().bulkInsert(schoolphotoUrl,arrayValues);
                Log.d("", "count ===== " + count);
            }
        }).start();
    }
}
